/**
 * 
 */
package cre.gutwo.imp;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import cre.gutwo.domain.CompanyCode;
import cre.gutwo.domain.DeviceInfo;
import cre.gutwo.util.PoiUtils;

/**
 * @author Cre.Gu
 *
 */
public class RuleImpUtils {

	public static List<CompanyCode> gainCompanyCodeList(String file) {
		List<CompanyCode> result = new LinkedList<CompanyCode>();
		try {

			InputStream is = new FileInputStream(file);
			Workbook wb = new XSSFWorkbook(is);
			Sheet sheet = wb.getSheetAt(0);

			for (Row row : sheet) {
				CompanyCode bean = new CompanyCode();

				for (Cell cell : row) {
					// 前提，所有表格格式一致，标题列顺序不变
					putValue(bean, cell);
				}

				result.add(bean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	public static List<DeviceInfo> gainDeviceInfoList(String file) {
		List<DeviceInfo> result = new LinkedList<DeviceInfo>();
		try {

			InputStream is = new FileInputStream(file);
			Workbook wb = new XSSFWorkbook(is);
			Sheet sheet = wb.getSheetAt(0);

			for (Row row : sheet) {
				DeviceInfo bean = new DeviceInfo();

				for (Cell cell : row) {
					// 前提，所有表格格式一致，标题列顺序不变
					putValue(bean, cell);
				}

				result.add(bean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	private static void putValue(DeviceInfo bean, Cell cell) {
		int i = cell.getColumnIndex();
		String value = PoiUtils.getStringCellValue(cell);

		switch (i) {
		case 0:
			bean.setDeviceName(value);
			break;
		case 1:
			bean.setLinkageID(value);
			break;
		case 2:
			bean.setMacAddr(value);
			break;
		case 3:
			bean.setIpAddr(value);
			break;
		case 4:
			bean.setUniqueKey(value);
			break;
		case 5:
			bean.setOs(value);
			break;
		case 6:
			bean.setOsLevel(value);
			break;
		default:
			break;
		}

	}

	private static void putValue(CompanyCode bean, Cell cell) {
		int i = cell.getColumnIndex();
		String value = PoiUtils.getStringCellValue(cell);

		switch (i) {
		case 0:
			bean.setName(value);
			break;
		case 1:
			bean.setCode(value);
			break;
		default:
			break;
		}
	}
}
